package com.zmn.oms.zmn.business.impl.ins;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.business.impl.messageV1.ZmnMcsSendUtil;
import com.zmn.oms.common.constant.InsConsts;
import com.zmn.oms.model.dto.messageV1.DingTalkMessageDIO;
import com.zmn.oms.model.dto.messageV1.SendMscDIO;
import com.zmn.oms.model.entity.ins.InsOrderMasterCpic;
import com.zmn.oms.services.interfaces.ins.InsOrderMasterCpicService;
import com.zmn.oms.zmn.business.interfaces.ins.InsCpicInsureBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2021/03/29 18:37
 */
@Slf4j
@Service("insCpicInsureBService")
public class InsCpicInsureBServiceImpl implements InsCpicInsureBService {

    @Autowired
    private InsOrderMasterCpicService insOrderMasterCpicService;

    /**
     * 修改出单状态
     * @param insMasterIds
     */
    @Override
    public void sendDingTalkMessageOnFailed(List<Long> insMasterIds) {
        if (insMasterIds == null || insMasterIds.isEmpty()) {
            return;
        }

        List<InsOrderMasterCpic> list = insOrderMasterCpicService.listByKeys(insMasterIds);
        if (list == null || list.isEmpty()) {
            return;
        }

        int templateLen = "工单号：；工程师姓名：；购买保单时间：；购买失败原因：；".length();
        StringBuilder contentBuilder = new StringBuilder();
        for (InsOrderMasterCpic item : list) {
            if (Objects.equals(InsOrderMasterCpic.FORM_STATUS_DONE, item.getFormStatus())) {
                continue;
            }
            String orderIdStr = String.valueOf(item.getOrderId());
            String masterName = Optional.ofNullable(item.getMasterName()).orElse("");
            String timeStr = DateUtil.toString(item.getRequestTime(), DateUtil.FORMAT_DEFAULT);
            String errorMsg = Optional.ofNullable(item.getMsg()).orElse("");

            int contentLen = contentBuilder.length();
            int currLen = templateLen + orderIdStr.length() + masterName.length()
                    + timeStr.length() + errorMsg.length();
            if ((contentLen + currLen) > 1000) {
                // 超过1000个字符，发送一条消息，将当前数据拼在下一条消息体里
                this.sendDingTalkMessage(contentBuilder.toString());
                contentBuilder = new StringBuilder();
            }

            contentBuilder.append("工单号：").append(orderIdStr).append("；")
                    .append("工程师姓名：").append(masterName).append("；")
                    .append("购买保单时间：").append(timeStr).append("；")
                    .append("购买失败原因：").append(errorMsg).append("；");
        }

        if (contentBuilder.length() > 0) {
            this.sendDingTalkMessage(contentBuilder.toString());
        }
    }


    private void sendDingTalkMessage(String content) {
        if (InsConsts.MSG_RULE_ID == null || InsConsts.DINGTALK_ID_FOR_RECIVE_MSG == null) {
            log.info("Send DingTalkMessage failed! InsConsts.MSG_RULE_ID=[{}], InsConsts.DINGTALK_ID_FOR_RECIVE_MSG=[{}]",
                    InsConsts.MSG_RULE_ID, InsConsts.DINGTALK_ID_FOR_RECIVE_MSG);
            return;
        }
        SendMscDIO sendMscDIO = new SendMscDIO();
        sendMscDIO.setRuleId(InsConsts.MSG_RULE_ID);// 消息ID：线上场景ID：10352，test2场景ID：10380
        DingTalkMessageDIO dingTaikParams = new DingTalkMessageDIO();
        sendMscDIO.setDingTalkParams(dingTaikParams);

        dingTaikParams.setDingTalkId(InsConsts.DINGTALK_ID_FOR_RECIVE_MSG); // 员工钉钉ID
        dingTaikParams.setParams(String.format("{\"msg_content\":\"%s\"}", content));

        ZmnMcsSendUtil.send(sendMscDIO);
    }
}
